package com.android.view.hqview;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;


import com.android.model.entity.eventbus.BusUtils;
import com.android.model.net.TimerHandler;
import com.mp.chart.listener.OnChartGestureListener;
import com.mp.chart.view.MaterialProgressDrawable;
import com.android.R;

import org.greenrobot.eventbus.EventBus;

/**
 * 共用部分
 */
public abstract class BaseChartView extends FrameLayout implements OnChartGestureListener {
    public final Bundle mBundle;

    private final TimerHandler mTimerHandler;
    protected boolean mActive = false;
    private MaterialProgressDrawable mProgress;
    private ProgressBar progressBar;//加载时的菊花
    private LinearLayout linearLayout;

    public BaseChartView(Context context, Bundle bundle) {
        super(context);
        mBundle = bundle;
        mTimerHandler = new TimerHandler(this);
        init(context);

    }

    public abstract int getLayoutId();

    private void init(Context context) {
        inflate(context, getLayoutId(), this);

        initWaiting(context);
        initLayout(context);

    }

    public abstract void initLayout(Context context);

    public abstract void initData();

    private void initWaiting(Context context) {

        LayoutParams layoutParams = new LayoutParams(50, 50);
        layoutParams.gravity = Gravity.CENTER;

        LayoutParams linearLayoutParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;

        progressBar = new ProgressBar(context);

        progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.trade_progressbar));

        linearLayout = new LinearLayout(context);

        linearLayout.setBackgroundColor(getResources().getColor(R.color.main_market_bg));

        addView(linearLayout,linearLayoutParams);

        addView(progressBar, layoutParams);

        stop();
    }

    public void stop() {

        progressBar.setVisibility(GONE);
        linearLayout.setVisibility(GONE);
    }

    public void start() {

        progressBar.setVisibility(VISIBLE);
        linearLayout.setVisibility(VISIBLE);
    }

    public void destroy() {

        stop();
    }

    @Override
    public void onChartLongPressed(MotionEvent me) {
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
//        QuoteStockInfoActivity.canRefresh = false;
//        Log.e("touch", "onChartLongPressed");
    }

    @Override
    public void onChartDoubleTapped(MotionEvent me) {
//        QuoteStockInfoActivity.canRefresh = false;
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
    }

    @Override
    public void onChartSingleTapped(MotionEvent me) {
//        QuoteStockInfoActivity.canRefresh = false;
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
        //单击操作，打开横屏
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//            ChartActivity.startActivity(getContext(), mBundle);
        }

    }

    @Override
    public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
//        QuoteStockInfoActivity.canRefresh = false;
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
//        Log.e("touch", "onChartFling");
    }

    @Override
    public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
//        QuoteStockInfoActivity.canRefresh = false;
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
//        Log.e("touch", "onChartScale");
    }

    @Override
    public void onChartTranslate(MotionEvent me, float dX, float dY) {
//        QuoteStockInfoActivity.canRefresh = false;
        EventBus.getDefault().post(new BusUtils.refreshStatus(false));
//        Log.e("touch", "onChartTranslate");
    }


    public void refreshData() {

    }

    /*1s刷新一次*/
    public void dispatchTimerHandler() {
        if (mActive) {
            mTimerHandler.sendEmptyMessageDelayed(0, 1000);
//            EventBus.getDefault().post(new BusUtils.refreshStatus(true));
        } else {//false下移除定时器
            mTimerHandler.removeMessages(0);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mTimerHandler.removeMessages(0);
    }

    public void setActive(boolean value) {

        mActive = value;

        if (!mActive) {
            mTimerHandler.removeMessages(0);
        }
    }

    public IndexView getIndexView(Context context){
        return new IndexView(context);
    }

    /**
     * 取消注册 子类实现该方法，请勿删除
     */
    public void unRegister() {
    }

    /**
     * 移动到最后点
     */
    public void moveToLast(){

    }

}
